library(haven)
library(dplyr)
library(MASS)
library(reshape2)
library(brant)
library(ordinal)

## WVS Wave 2 China ##
WV2_Data <- read_dta("WV2_Data.dta")
cn2<-filter(WV2_Data, V2==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn2$postmat<-NA
cn2$postmat[which((cn2$V259==1 & cn2$V260==3) | (cn2$V259==3 & cn2$V260==1))]<-0
cn2$postmat[which((cn2$V259==1 & cn2$V260==2) | (cn2$V259==1 & cn2$V260==4) | (cn2$V259==3 & cn2$V260==2) | (cn2$V259==3 & cn2$V260==4) | (cn2$V259==2 & cn2$V260==1) | (cn2$V259==2 & cn2$V260==3) | (cn2$V259==4 & cn2$V260==1) | (cn2$V259==4 & cn2$V260==3))]<-1
cn2$postmat[which((cn2$V259==2 & cn2$V260==4) | (cn2$V259==4 & cn2$V260==2))]<-2
table(cn2$postmat)
round(prop.table(table(cn2$postmat)),digits = 3)
mean(cn2$postmat, na.rm = T)

## WVS Wave 3 China ##
WV3_Data <- read_dta("WV3_Data.dta")
cn3<-filter(WV3_Data, V2==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn3$postmat<-NA
cn3$postmat[which((cn3$V106==1 & cn3$V107==3) | (cn3$V106==3 & cn3$V107==1))]<-0
cn3$postmat[which((cn3$V106==1 & cn3$V107==2) | (cn3$V106==1 & cn3$V107==4) | (cn3$V106==3 & cn3$V107==2) | (cn3$V106==3 & cn3$V107==4) | (cn3$V106==2 & cn3$V107==1) | (cn3$V106==2 & cn3$V107==3) | (cn3$V106==4 & cn3$V107==1) | (cn3$V106==4 & cn3$V107==3))]<-1
cn3$postmat[which((cn3$V106==2 & cn3$V107==4) | (cn3$V106==4 & cn3$V107==2))]<-2
table(cn3$postmat)
round(prop.table(table(cn3$postmat)),digits = 3)
mean(cn3$postmat, na.rm = T)

## WVS Wave 4 China ##
WV4_Data <- read_dta("WV4_Data.dta")
cn4<-filter(WV4_Data, V2==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn4$postmat<-NA
cn4$postmat[which((cn4$V122==1 & cn4$V123==3) | (cn4$V122==3 & cn4$V123==1))]<-0
cn4$postmat[which((cn4$V122==1 & cn4$V123==2) | (cn4$V122==1 & cn4$V123==4) | (cn4$V122==3 & cn4$V123==2) | (cn4$V122==3 & cn4$V123==4) | (cn4$V122==2 & cn4$V123==1) | (cn4$V122==2 & cn4$V123==3) | (cn4$V122==4 & cn4$V123==1) | (cn4$V122==4 & cn4$V123==3))]<-1
cn4$postmat[which((cn4$V122==2 & cn4$V123==4) | (cn4$V122==4 & cn4$V123==2))]<-2
table(cn4$postmat)
round(prop.table(table(cn4$postmat)),digits = 3)
mean(cn4$postmat, na.rm = T)

## WVS Wave 5 China ##
WV5_Data <- read_dta("WV5_Data.dta")
cn5<-filter(WV5_Data, V2==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn5$postmat<-NA
cn5$postmat[which((cn5$V71==1 & cn5$V72==3) | (cn5$V71==3 & cn5$V72==1))]<-0
cn5$postmat[which((cn5$V71==1 & cn5$V72==2) | (cn5$V71==1 & cn5$V72==4) | (cn5$V71==3 & cn5$V72==2) | (cn5$V71==3 & cn5$V72==4) | (cn5$V71==2 & cn5$V72==1) | (cn5$V71==2 & cn5$V72==3) | (cn5$V71==4 & cn5$V72==1) | (cn5$V71==4 & cn5$V72==3))]<-1
cn5$postmat[which((cn5$V71==2 & cn5$V72==4) | (cn5$V71==4 & cn5$V72==2))]<-2
table(cn5$postmat)
round(prop.table(table(cn5$postmat)),digits = 3)
mean(cn5$postmat, na.rm = T)

## WVS Wave 6 China ##
WV6_Data <- read_dta("WV6_Data.dta")
cn6<-filter(WV6_Data, V2==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn6$postmat<-NA
cn6$postmat[which((cn6$V62==1 & cn6$V63==3) | (cn6$V62==3 & cn6$V63==1))]<-0
cn6$postmat[which((cn6$V62==1 & cn6$V63==2) | (cn6$V62==1 & cn6$V63==4) | (cn6$V62==3 & cn6$V63==2) | (cn6$V62==3 & cn6$V63==4) | (cn6$V62==2 & cn6$V63==1) | (cn6$V62==2 & cn6$V63==3) | (cn6$V62==4 & cn6$V63==1) | (cn6$V62==4 & cn6$V63==3))]<-1
cn6$postmat[which((cn6$V62==2 & cn6$V63==4) | (cn6$V62==4 & cn6$V63==2))]<-2
table(cn6$postmat)
round(prop.table(table(cn6$postmat)),digits = 3)
mean(cn6$postmat, na.rm = T)

## WVS Wave 7 China ##
WV7_Data <- read_dta("WV7_Data.dta")
cn7<-filter(WV7_Data, B_COUNTRY==156)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
cn7$postmat<-NA
cn7$postmat[which((cn7$Q154==1 & cn7$Q155==3) | (cn7$Q154==3 & cn7$Q155==1))]<-0
cn7$postmat[which((cn7$Q154==1 & cn7$Q155==2) | (cn7$Q154==1 & cn7$Q155==4) | (cn7$Q154==3 & cn7$Q155==2) | (cn7$Q154==3 & cn7$Q155==4) | (cn7$Q154==2 & cn7$Q155==1) | (cn7$Q154==2 & cn7$Q155==3) | (cn7$Q154==4 & cn7$Q155==1) | (cn7$Q154==4 & cn7$Q155==3))]<-1
cn7$postmat[which((cn7$Q154==2 & cn7$Q155==4) | (cn7$Q154==4 & cn7$Q155==2))]<-2
table(cn7$postmat)
round(prop.table(table(cn7$postmat)),digits = 3)
mean(cn7$postmat, na.rm = T)

## WVS Wave 3 TW ##
tw3<-filter(WV3_Data, V2==158)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
tw3$postmat<-NA
tw3$postmat[which((tw3$V106==1 & tw3$V107==3) | (tw3$V106==3 & tw3$V107==1))]<-0
tw3$postmat[which((tw3$V106==1 & tw3$V107==2) | (tw3$V106==1 & tw3$V107==4) | (tw3$V106==3 & tw3$V107==2) | (tw3$V106==3 & tw3$V107==4) | (tw3$V106==2 & tw3$V107==1) | (tw3$V106==2 & tw3$V107==3) | (tw3$V106==4 & tw3$V107==1) | (tw3$V106==4 & tw3$V107==3))]<-1
tw3$postmat[which((tw3$V106==2 & tw3$V107==4) | (tw3$V106==4 & tw3$V107==2))]<-2
table(tw3$postmat)
round(prop.table(table(tw3$postmat)),digits = 3)
mean(tw3$postmat, na.rm = T)

## WVS Wave 5 TW ##
tw5<-filter(WV5_Data, V2==158)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
tw5$postmat<-NA
tw5$postmat[which((tw5$V71==1 & tw5$V72==3) | (tw5$V71==3 & tw5$V72==1))]<-0
tw5$postmat[which((tw5$V71==1 & tw5$V72==2) | (tw5$V71==1 & tw5$V72==4) | (tw5$V71==3 & tw5$V72==2) | (tw5$V71==3 & tw5$V72==4) | (tw5$V71==2 & tw5$V72==1) | (tw5$V71==2 & tw5$V72==3) | (tw5$V71==4 & tw5$V72==1) | (tw5$V71==4 & tw5$V72==3))]<-1
tw5$postmat[which((tw5$V71==2 & tw5$V72==4) | (tw5$V71==4 & tw5$V72==2))]<-2
table(tw5$postmat)
round(prop.table(table(tw5$postmat)),digits = 3)
mean(tw5$postmat, na.rm = T)

## WVS Wave 6 TW ##
tw6<-filter(WV6_Data, V2==158)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
tw6$postmat<-NA
tw6$postmat[which((tw6$V62==1 & tw6$V63==3) | (tw6$V62==3 & tw6$V63==1))]<-0
tw6$postmat[which((tw6$V62==1 & tw6$V63==2) | (tw6$V62==1 & tw6$V63==4) | (tw6$V62==3 & tw6$V63==2) | (tw6$V62==3 & tw6$V63==4) | (tw6$V62==2 & tw6$V63==1) | (tw6$V62==2 & tw6$V63==3) | (tw6$V62==4 & tw6$V63==1) | (tw6$V62==4 & tw6$V63==3))]<-1
tw6$postmat[which((tw6$V62==2 & tw6$V63==4) | (tw6$V62==4 & tw6$V63==2))]<-2
table(tw6$postmat)
round(prop.table(table(tw6$postmat)),digits = 3)
mean(tw6$postmat, na.rm = T)

## WVS Wave 7 TW ##
tw7<-filter(WV7_Data, B_COUNTRY==158)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
tw7$postmat<-NA
tw7$postmat[which((tw7$Q154==1 & tw7$Q155==3) | (tw7$Q154==3 & tw7$Q155==1))]<-0
tw7$postmat[which((tw7$Q154==1 & tw7$Q155==2) | (tw7$Q154==1 & tw7$Q155==4) | (tw7$Q154==3 & tw7$Q155==2) | (tw7$Q154==3 & tw7$Q155==4) | (tw7$Q154==2 & tw7$Q155==1) | (tw7$Q154==2 & tw7$Q155==3) | (tw7$Q154==4 & tw7$Q155==1) | (tw7$Q154==4 & tw7$Q155==3))]<-1
tw7$postmat[which((tw7$Q154==2 & tw7$Q155==4) | (tw7$Q154==4 & tw7$Q155==2))]<-2
table(tw7$postmat)
round(prop.table(table(tw7$postmat)),digits = 3)
mean(tw7$postmat, na.rm = T)

## WVS Wave 6 HK ##
hk6<-filter(WV6_Data, V2==344)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
hk6$postmat<-NA
hk6$postmat[which((hk6$V62==1 & hk6$V63==3) | (hk6$V62==3 & hk6$V63==1))]<-0
hk6$postmat[which((hk6$V62==1 & hk6$V63==2) | (hk6$V62==1 & hk6$V63==4) | (hk6$V62==3 & hk6$V63==2) | (hk6$V62==3 & hk6$V63==4) | (hk6$V62==2 & hk6$V63==1) | (hk6$V62==2 & hk6$V63==3) | (hk6$V62==4 & hk6$V63==1) | (hk6$V62==4 & hk6$V63==3))]<-1
hk6$postmat[which((hk6$V62==2 & hk6$V63==4) | (hk6$V62==4 & hk6$V63==2))]<-2
table(hk6$postmat)
round(prop.table(table(hk6$postmat)),digits = 3)
mean(hk6$postmat, na.rm = T)

## WVS Wave 7 HK ##
hk<-filter(WV7_Data, B_COUNTRY==344)
## Define postmat, which represents the postmaterialism index for individual Hongkongers. 1=most materialist, 4=most postmaterialist ##
hk$postmat<-NA
hk$postmat[which((hk$Q154==1 & hk$Q155==3) | (hk$Q154==3 & hk$Q155==1))]<-0
hk$postmat[which((hk$Q154==1 & hk$Q155==2) | (hk$Q154==1 & hk$Q155==4) | (hk$Q154==3 & hk$Q155==2) | (hk$Q154==3 & hk$Q155==4) | (hk$Q154==2 & hk$Q155==1) | (hk$Q154==2 & hk$Q155==3) | (hk$Q154==4 & hk$Q155==1) | (hk$Q154==4 & hk$Q155==3))]<-1
hk$postmat[which((hk$Q154==2 & hk$Q155==4) | (hk$Q154==4 & hk$Q155==2))]<-2
table(hk$postmat)
round(prop.table(table(hk$postmat)),digits = 3)
mean(hk$postmat, na.rm = T)

## WVS waves countries ##
library(readxl)
country <- read_excel("C:/Users/theod/Desktop/dissertation/postmaterialism/postmat_country.xlsx")

## Figure 3a ##
ggplot(country, aes(x = wave, y = materialism, group = country)) +
  geom_line(size=2, aes(linetype=country)) + geom_point(size=4) + ggtitle("Materialism") +
  scale_linetype_manual(values = c("solid","dotted","twodash")) +
  scale_y_continuous(limit = c(0.1,0.8)) +
  theme(axis.text = element_text(size=35),
        axis.title.x = element_text(size=35),
        axis.title.y = element_blank(),
        title = element_blank(),
        legend.text = element_text(size=35),
        legend.title = element_blank(),
        legend.key.size = unit(2, 'cm'))

## Figure 3b ##
ggplot(country, aes(x = wave, y = mixed, group = country)) +
  geom_line(size=2, aes(linetype=country)) + geom_point(size=4) + ggtitle("Mixed materialism and postmaterialism") +
  scale_linetype_manual(values = c("solid","dotted","twodash")) +
  scale_y_continuous(limit = c(0.2,0.7)) +
  theme(axis.text = element_text(size=35),
        axis.title.x = element_text(size=35),
        axis.title.y = element_blank(),
        title = element_blank(),
        legend.text = element_text(size=35),
        legend.title = element_blank(),
        legend.key.size = unit(2, 'cm'))

## Figure 3c ##
ggplot(country, aes(x = wave, y = postmaterialism, group = country)) +
  geom_line(size=2, aes(linetype=country)) + geom_point(size=4) + ggtitle("Postmaterialism") +
  scale_linetype_manual(values = c("solid","dotted","twodash")) +
  scale_y_continuous(limit = c(0,0.25)) +
  theme(axis.text = element_text(size=35),
        axis.title.x = element_text(size=35),
        axis.title.y = element_blank(),
        title = element_blank(),
        legend.text = element_text(size=35),
        legend.title = element_blank(),
        legend.key.size = unit(2, 'cm'))

## Figure 3d ##
library(ggplot2)
ggplot(country, aes(x = wave, y = mean, group = country)) +
  geom_line(size=2, aes(linetype=country)) + geom_point(size=4) + ggtitle("Mean of postmaterialism") +
  scale_linetype_manual(values = c("solid","dotted","twodash")) +
  scale_y_continuous(limit = c(0,1)) +
  theme(axis.text = element_text(size=35),
        axis.title.x = element_text(size=35),
        axis.title.y = element_blank(),
        title = element_blank(),
        legend.text = element_text(size=35),
        legend.title = element_blank(),
        legend.key.size = unit(2, 'cm'))